package com.hongxun.budget.ui.fragment;

import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hongxun.budget.R;
import com.hongxun.budget.utils.BannerUtils;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.ScreenUtils;
import com.hongxun.entity.HomeItem;
import com.hongxun.entity.HomeLabel;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;
import com.wenld.wenldbanner.WenldBanner;
import com.yuyh.library.imgsel.widget.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by admin on 2018/7/19.
 */

public class MainFragment extends BaseFragment {
    @BindView(R.id.center_title)
    TextView centerTitle;
    @BindView(R.id.commonBanner)
    WenldBanner commonBanner;
    @BindView(R.id.list)
    RecyclerView list;
    List<Integer> bannerList = new ArrayList<>();
    List<HomeLabel> datas = new ArrayList<>();
    SuperAdapter<HomeLabel> adapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    public void initView() {
        centerTitle.setText("首页");
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(context), ScreenUtils.getScreenWidth(context) / 3);
        commonBanner.setLayoutParams(layoutParams);
        bannerList.add(R.mipmap.banner1);
        BannerUtils.setBannerString(context, bannerList, commonBanner);
        list.setLayoutManager(RecyclerViewUtils.getLinearLayoutManagerForVertical(context));
//        list.addItemDecoration(new DividerItemDecoration(context,DividerItemDecoration.VERTICAL));
        datas.addAll(HomeLabel.getInstance(context));
        list.setAdapter(adapter = new SuperAdapter<HomeLabel>(R.layout.content_fragment_home_list) {
            @Override
            public void bindView(ViewHolder viewHolder, HomeLabel item, int i) {
                ((TextView) viewHolder.getView(R.id.label)).setText(item.getLabel());
                RecyclerView list = viewHolder.getView(R.id.list);
                list.setLayoutManager(RecyclerViewUtils.getGridLayoutManager(context, 3));
                list.addItemDecoration(new DividerGridItemDecoration(context));
                SuperAdapter<HomeItem> adapter = new SuperAdapter<HomeItem>(R.layout.content_fragment_home) {
                    @Override
                    public void bindView(ViewHolder viewHolder, HomeItem homeItem, int i) {
                        Glide.with(context).load(homeItem.getImgRes()).into((ImageView) viewHolder.getView(R.id.image_top));
                        ((TextView) viewHolder.getView(R.id.text)).setText(homeItem.getTitle());
                        viewHolder.getView(R.id.layout).setOnClickListener(homeItem.getListener());
                    }
                };
                list.setAdapter(adapter);
                adapter.setData(item.getItems());
            }
        });
        adapter.setData(datas);
    }

}
